﻿Imports System.Data.SqlClient
Public Class FormEmployee

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        FormEmployeeEditor.ShowDialog()
    End Sub

    Private Sub FormEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshData()
    End Sub
    Private Sub RefreshData()
        Cmd.Parameters("@s") = "%" & txtSearch.Text & "%"
        Me.dgv.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_EMPLOYEE WHERE Active =1 AND UPPER (Employee_Name) LIKE UPPER('%'+@S+'%')")
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        RefreshData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show(My.Resources.AreYouSureWantToDelete, My.Resources.Delete, MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If

        Dim row = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("Employee_Id").Value

        Cmd.Parameters("@Employee_Id") = id
        Cmd.ExecuteNonQuery("UPDATE TBL_EMPLOYEE SET Active =0 WHERE Employee_Id =@Employee_Id")
        RefreshData()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.dgv.SelectedRows(0)
        Dim frm As New FormEmployeeEditor()

        frm.txtEmployee_Id.Text = row.Cells("Employee_Id").Value
        frm.txtNameEm.Text = row.Cells("Employee_Name").Value

        frm.rdoF.Checked = (row.Cells("Sex").Value = "ស្រី")
        frm.rdoM.Checked = Not frm.rdoF.Checked
        frm.dtpDOB.Text = row.Cells("Day_Of_Birth").Value
        frm.txtPOB.Text = row.Cells("Place_Of_Birth").Value
        frm.txtPhone.Text = row.Cells("Phone").Value
        frm.txtAddress.Text = row.Cells("Address").Value
        frm.txtSalary.Text = row.Cells("Salary").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RefreshData()
        End If
    End Sub
End Class